# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=openal
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.21.0
pkgrel=2
pkgdesc="OpenAL audio library for use with opengl (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
url="https://github.com/kcat/openal-soft"
license=('GPL2')
depends=("${MINGW_PACKAGE_PREFIX}-libmysofa")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-fluidsynth"
             "${MINGW_PACKAGE_PREFIX}-portaudio")
options=('strip' 'staticlibs')
source=(https://github.com/kcat/openal-soft/archive/openal-soft-${pkgver}.tar.gz
        0001-versioned-w32-dll.mingw.patch
        0002-w32ize-portaudio-loading.mingw.patch
        0003-openal-not-32.mingw.patch
        0004-disable-OSS-windows.patch
        0005-mingw-dont-check-libm.patch
        0006-mingw-use-own-import-library-winmm.patch)
sha256sums=('cd3650530866f3906058225f4bfbe0052be19e0a29dcc6df185a460f9948feec'
            'c3e56b5594ada0b95588373bd8ca062677805beae393c6551301518ab24a0cd3'
            'f0bde7d3a8087530ab1bb2f48e59eec6f4cf60d59c5afe3a0c28c95e6b138751'
            '33dccfc603a48d265341e60bc801976d077e546ab5c91a93e8426eac01bb7ad4'
            '8b8ff007d224a5267aead184dc6d24317e40be69c91064cdfdc35b78bb762cc6'
            'c2528ecc034ea993cb63893b3f2ce5ceeac8d63295decf6864f58cd646c74299'
            '581af86e2191957bd8a8e5c6813a20c42eafeb82c2f2eb1c170f94507aa3fd5c')

prepare() {
  cd openal-soft-openal-soft-${pkgver}
  patch -p1 -i ${srcdir}/0001-versioned-w32-dll.mingw.patch
  patch -p1 -i ${srcdir}/0002-w32ize-portaudio-loading.mingw.patch
  patch -p1 -i ${srcdir}/0003-openal-not-32.mingw.patch
  patch -p1 -i ${srcdir}/0004-disable-OSS-windows.patch
  patch -p1 -i ${srcdir}/0005-mingw-dont-check-libm.patch
  patch -p1 -i ${srcdir}/0006-mingw-use-own-import-library-winmm.patch
}

build() {
  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  [[ -d "${srcdir}"/shared-${MINGW_CHOST} ]] && rm -rf ${srcdir}/shared-${MINGW_CHOST}
  mkdir -p "${srcdir}/shared-${MINGW_CHOST}" && cd "${srcdir}/shared-${MINGW_CHOST}"

  DXSDK_DIR=${MINGW_PREFIX}/${MINGW_CHOST} \
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    "${extra_config[@]}" \
    -DALSOFT_EXAMPLES=OFF \
    -DALSOFT_UTILS=ON \
    -DALSOFT_NO_CONFIG_UTIL=ON \
    -DLIBTYPE=SHARED \
    -DDSOUND_INCLUDE_DIR=${MINGW_PREFIX}/${MINGW_CHOST}/include \
    -DDSOUND_LIBRARY=${MINGW_PREFIX}/${MINGW_CHOST}/lib/libdsound.a \
    -DWINMM_LIBRARY=${MINGW_PREFIX}/${MINGW_CHOST}/lib/libwinmm.a \
    ../openal-soft-openal-soft-${pkgver}
  make

  [[ -d "${srcdir}"/static-${MINGW_CHOST} ]] && rm -rf ${srcdir}/static-${MINGW_CHOST}
  mkdir -p "${srcdir}/static-${MINGW_CHOST}" && cd "${srcdir}/static-${MINGW_CHOST}"

  # enabling utilities on static causes an undefined reference error.
  DXSDK_DIR=${MINGW_PREFIX}/${MINGW_CHOST} \
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    "${extra_config[@]}" \
    -DALSOFT_EXAMPLES=OFF \
    -DALSOFT_UTILS=OFF \
    -DALSOFT_NO_CONFIG_UTIL=ON \
    -DLIBTYPE=STATIC \
    -DDSOUND_INCLUDE_DIR=${MINGW_PREFIX}/${MINGW_CHOST}/include \
    -DDSOUND_LIBRARY=${MINGW_PREFIX}/${MINGW_CHOST}/lib/libdsound.a \
    -DWINMM_LIBRARY=${MINGW_PREFIX}/${MINGW_CHOST}/lib/libwinmm.a \
    ../openal-soft-openal-soft-${pkgver}
  make
}

package() {
  cd "${srcdir}/static-${MINGW_CHOST}"
  make DESTDIR=${pkgdir} install

  cd "${srcdir}/shared-${MINGW_CHOST}"
  make DESTDIR=${pkgdir} install
}
